package com.coding.controller;

import com.coding.base.pojo.BaseModel;
import com.coding.service.OssImagesService;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;


/**
 * 图片上传
 *
 * @author wangchenghao
 * @date 7月25日
 */
@Controller
@ResponseBody
@RequestMapping("image")
public class ImageController {
    public static final Logger logger = LoggerFactory.getLogger(ImageController.class);
    @Autowired
    private OssImagesService ossImagesService;


    @ApiOperation(value = "上传图片至oss")
    @RequestMapping(value = "/upImage", method = RequestMethod.POST, consumes = "multipart/*", headers = "content-type=multipart/form-data")
    @ResponseBody
    public BaseModel upimage(@ApiParam(value = "上传的文件", required = true) MultipartFile file, HttpServletRequest request) {
        BaseModel model = new BaseModel();
        try {
            ossImagesService.setOssimage(file, model);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return model;
    }

    @ApiOperation(value = "多张图片至oss")
    @RequestMapping(value = "/upImages", method = RequestMethod.POST, consumes = "multipart/*", headers = "content-type=multipart/form-data")
    @ResponseBody
    public BaseModel upimagemore(@RequestParam(value = "files", required = true) MultipartFile[] files, HttpServletRequest request) {
        BaseModel model = new BaseModel();
        try {
            for (MultipartFile file : files
            ) {
                ossImagesService.setOssimage(file, model);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return model;
    }

    @ApiOperation(value = "通过id查询图片资源")
    @RequestMapping(value = "/getImageById", method = {RequestMethod.GET})
    public BaseModel upimage(@ApiParam(value = "图片id", required = true) @RequestParam("image_id") int image_id) {
        BaseModel model = new BaseModel();
        try {
            ossImagesService.getImgbyid(image_id, model);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return model;
    }

    @ApiOperation(value = "通过name查询图片资源")
    @RequestMapping(value = "/getImageByName", method = {RequestMethod.GET})
    public BaseModel getimagebyname(@ApiParam(value = "图片name", required = true) @RequestParam("image_name") String image_name) {
        BaseModel model = new BaseModel();
        try {
            ossImagesService.getImagebyname(image_name, model);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return model;
    }

    @ApiOperation(value = "通过tag查询图片资源")
    @RequestMapping(value = "/getImageByTag", method = {RequestMethod.GET})
    public BaseModel getimagebytag(@ApiParam(value = "图片tag", required = true) @RequestParam("image_tag") String image_tag) {
        BaseModel model = new BaseModel();
        try {
            ossImagesService.getImagebytag(image_tag, model);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return model;
    }
}
